package com.anxin.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.anxin.util.OVCommonUtil;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class TextBufferedImage extends HttpServlet
{
	private static final long serialVersionUID = 1L;

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        doGet(request, response);
    }

    
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
    {
    	HttpSession session = req.getSession();
        String randomPassword = OVCommonUtil.getRandom(4);
    	session.setAttribute("randomCode", randomPassword);
      

        OutputStream out = resp.getOutputStream();

        try
        {
            JPEGImageEncoder encode = JPEGCodec.createJPEGEncoder(out);
            BufferedImage bi = CreateBufferedImage(randomPassword);
            encode.encode(bi);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    private BufferedImage CreateBufferedImage(String randomPassword)
    {
        int width = 80;
        int height = 40;

        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        Graphics2D g2d = bufferedImage.createGraphics();
        g2d.clearRect(0, 0, width, height);
        g2d.fillRect(0, 0, width, height);
        
    
        g2d.setColor(getRandColor(160,200));
        g2d.setFont(getFont());
        Random random = new Random();
        for (int i=0;i<155;i++)
        {
        	int x = random.nextInt(width);
        	int y = random.nextInt(height);
            int xl = random.nextInt(12);
            int yl = random.nextInt(12);
            g2d.drawLine(x,y,x+xl,y+yl);
        }
        StringBuffer sRand = new StringBuffer();
        for (int i=0;i<4;i++){
            String rand = randomPassword.substring(i, i+1);
            sRand.append(rand);
            g2d.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
        }
        
        g2d.dispose();
        return bufferedImage;

    }
    
    private Color getRandColor(int fc, int bc) {
		Random random = new Random();
		if (fc > 255)
			fc = 255;
		if (bc > 255)
			bc = 255;
		int r = fc + random.nextInt(bc - fc);
		int g = fc + random.nextInt(bc - fc);
		int b = fc + random.nextInt(bc - fc);
		return new Color(r, g, b);
	}
    
    private Font getFont() {
		Random random = new Random();
		Font font[] = new Font[5];
		font[0] = new Font("Ravie", Font.PLAIN, 25);
		font[1] = new Font("Antique Olive Compact", Font.PLAIN, 25);
		font[2] = new Font("Forte", Font.PLAIN, 25);
		font[3] = new Font("Wide Latin", Font.PLAIN, 25);
		font[4] = new Font("Gill Sans Ultra Bold", Font.PLAIN, 23);
		return font[random.nextInt(5)];
	}

}